//
//  KLStatusTableViewCell.swift
//  BCWeiBo
//
//  Created by bcmac3 on 16/4/5.
//  Copyright © 2016年 KellenYangs. All rights reserved.
//

import UIKit
import SDWebImage
import KILabel

/// 微博 Cell 的标示符枚举
enum StatusCellIdentifier: String
{
    case NormalCell = "NormalCell"
    case ForwardCell = "ForwardCell"
    
    /// 静态函数, 相当于类方法
    /// 根据微博数据，返回对应的标示符
    static func cellId(status: KLStatus) ->String
    {
        return status.retweeted_status != nil ? StatusCellIdentifier.ForwardCell.rawValue : StatusCellIdentifier.NormalCell.rawValue
    }
}

class KLStatusTableViewCell: UITableViewCell {

    /// 配图约束宽度
    var widthCons: NSLayoutConstraint?
    /// 配图约束高度
    var heightCons: NSLayoutConstraint?
    /// 配图顶部约束
    var topCons: NSLayoutConstraint?
    
    var status: KLStatus?{
        didSet{
            // 设置顶部视图
            topView.status = status
            
            // 设置正文
            contentLabel.text = status!.text
            contentLabel.attributedText = KLEmoticonPackage.emoticonString(status!.text ?? "", font: contentLabel.font)
            
            // 设置配图视图数据
            //            pictureView.status = status
            pictureView.status = status?.retweeted_status != nil ?  status?.retweeted_status :  status
            
            // 设置配图宽高
            widthCons?.constant = pictureView.bounds.size.width
            heightCons?.constant = pictureView.bounds.size.height
            topCons?.constant = (pictureView.bounds.size.height == 0) ? 0 : 10
            
            // 刷新表格
            pictureView.reloadData()
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        // 初始化界面
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder)
    {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI()
    {
        // 1.添加子控件
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(footerView)
        
        // 2.布局子控件
        // 顶部视图
        let width = UIScreen.mainScreen().bounds.size.width
        topView.xmg_AlignInner(type: XMG_AlignType.TopLeft, referView: contentView, size: CGSize(width: width, height: 55))
        
        // 底部视图
        footerView.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: width, height: 44), offset: CGPoint(x: -10, y: 10))
        
        
    }
    // 计算行高
    func rowHeight(status: KLStatus) -> CGFloat{
        // 1.设置模型, 以便于调用didSet方法
        self.status = status
        
        // 2.强制更新布局
        layoutIfNeeded()
        
        // 3.返回行高
        return CGRectGetMaxY(footerView.frame)
    }
    
    
    // MARK: - 懒加载
    /// 正文
    var contentLabel: KILabel = {
        let label = KILabel()
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(15)
        label.numberOfLines = 0
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - CGFloat(2 * 10)
        
        label.urlLinkTapHandler = {
            (label, string, range)
            in
            print(string)
        }
        
        return label
    }()
    /// 顶部视图
    lazy var topView: KLStatusTopView = KLStatusTopView()
    /// 配图视图
    lazy var pictureView: KLStatusPictureView = KLStatusPictureView()
    /// 底部视图
    lazy var footerView: KLStatusBottomView = KLStatusBottomView()
}
